package com.wimcorp.magic.cost;

import java.rmi.RemoteException;

import javax.persistence.Entity;

import com.wimcorp.magic.board.CardAccessor;
import com.wimcorp.magic.board.Sacrifice;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.exception.CostException;
import com.wimcorp.magic.input.Location;
import com.wimcorp.magic.input.Target;
import com.wimcorp.magic.input.Type;

@Entity
public class SacrificeCreature extends Cost {

	public SacrificeCreature() {
	}

	public SacrificeCreature(SacrificeCreature cost) {
		super(cost);
	}

	public void payCost() throws CostException {
		try {
			Creature creature = null;
			Sacrifice sacrifice = getPlayer().getSummoningPlaines().getSacrifice();
			if (sacrifice.size() > 0) {
				creature = (Creature) sacrifice.getCards().iterator().next();
				sacrifice.remove(creature);
			} else {
				long id = getPlayer().getUserInterface().select(Target.SELF, Type.CREATURE, Location.BATTLEFIELD);
				creature = (Creature) CardAccessor.getCardAccessor(getPlayer(), Location.BATTLEFIELD.toString())
						.getCard(id);
				if (creature == null) {
					throw new CostException(String.format("Creature with id %s was not found on the Battlefield.", id));
				}
			}
			creature.graveyard();

		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public String toString() {
		return "Sacrifice a creature";

	}

	@Override
	public SacrificeCreature clone() {
		return new SacrificeCreature(this);
	}
}
